//
//  JYJVCOfSet.m
//  HappyListen
//
//  Created by 冀永金 on 16/8/29.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJVCOfSet.h"
#import "JYJTAMacro.h"
#import "Masonry.h"
#import "BaseLabel.h"
#import "JYJVCOfSetContent.h"
#import "JYJMusicMiniView.h"
@interface JYJVCOfSet ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, retain) NSString *imgPath;

@property (nonatomic, strong) UIImageView *backImage;
@property (nonatomic, strong)  UISegmentedControl *seg;
@property (nonatomic, strong) UIImageView *genderImage;
@property (nonatomic, strong)  UIImageView *imageOfHead;
@property (nonatomic, strong)  UILabel *gender;

@property (nonatomic, strong) UILabel *age;
@property (nonatomic, strong) UILabel *bithday;
@property (nonatomic, strong) UILabel *constellation ;
@property (nonatomic, strong) BaseLabel *nameLabel;
@end

@implementation JYJVCOfSet

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createViews];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
   
    // Do any additional setup after loading the view.
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [JYJMusicMiniView shareMiniView].hidden = YES;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [JYJMusicMiniView shareMiniView].hidden = YES;
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
}
- (void)createViews {
    self.backImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mohu "]];
    [self.view addSubview:_backImage];
    self.backImage.frame = CGRectMake(0, 0,SCREEN_WIDTH , 220);
    _backImage.userInteractionEnabled = YES;
    self.imageOfHead = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"default_user"]];
    [self.backImage addSubview:_imageOfHead];
    _imageOfHead.userInteractionEnabled = YES;
    [_imageOfHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(30);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
        make.left.equalTo(self.view.mas_left).with.offset(SCREEN_WIDTH / 2 - 40);
    }];
    _imageOfHead.layer.cornerRadius = 40;
    _imageOfHead.clipsToBounds = YES;
    [_imageOfHead.layer setBorderWidth:2];// 设置边界宽度
    _imageOfHead.layer.borderColor = [UIColor colorWithRed:0.93 green:0.14 blue:0.14 alpha:1.00].CGColor;// 设置边界颜色
    // 加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handlePic:)];
    [_imageOfHead addGestureRecognizer:tap];
    self.imageOfHead.userInteractionEnabled = YES;
    //创建单例
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([userDefaults dataForKey:@"image"]) {
        [_imageOfHead setImage:[UIImage imageWithData:[userDefaults dataForKey:@"image"]]];
    
    }
    
    self.nameLabel = [[BaseLabel alloc]init];
    [self.backImage addSubview:_nameLabel];
    _nameLabel.textAlignment = 1;
    if (self.infoDic) {
        NSDictionary *dicOfUser = [self.infoDic objectForKey:@"user"];
        _nameLabel.text = [dicOfUser objectForKey:@"nickname"];
        
    }else if (self.dic){
        NSDictionary *dicOfUser = [self.dic objectForKey:@"user"];
         _nameLabel.text = [dicOfUser objectForKey:@"nickname"];
    }else {
        NSDictionary *dicOfUser = [self.infoDicTwo objectForKey:@"user"];
        _nameLabel.text = [dicOfUser objectForKey:@"nickname"];

    }
    _nameLabel.textColor = [UIColor whiteColor];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_imageOfHead.mas_bottom).with.offset(15);
        make.left.equalTo(self.view.mas_left).with.offset(20);
        make.right.equalTo(self.view.mas_right).with.offset(-20);
        make.height.mas_equalTo(40);
    }];
    self.genderImage = [[UIImageView alloc]init];
    [self.backImage addSubview:_genderImage];
    [self.genderImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nameLabel.mas_bottom).with.offset(10);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
        make.left.equalTo(self.view.mas_left).with.offset(SCREEN_WIDTH / 2 - 10);
    }];
    UIButton *buttonOfSet = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backImage addSubview:buttonOfSet];
    [buttonOfSet setImage:[UIImage imageNamed:@"my_setting"] forState:UIControlStateNormal];
    [buttonOfSet mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(20);
        make.left.equalTo(self.view.mas_left).with.offset(20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(60);
    }];
    [buttonOfSet addTarget:self action:@selector(handle:) forControlEvents:UIControlEventTouchUpInside];
    self.seg = [[UISegmentedControl alloc]initWithItems:@[@"资料",@"动态"]];
    
    [_seg setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
    _seg.tintColor =[UIColor lightGrayColor];
    _seg.backgroundColor = [UIColor colorWithRed:0.09 green:0.09 blue:0.11 alpha:1.00];
    _seg.frame = CGRectMake(0, 220, SCREEN_WIDTH, 50);
    [self.view addSubview:_seg];
    [_seg addTarget:self action:@selector(handleValue:) forControlEvents:UIControlEventValueChanged];

}

- (void)setDic:(NSDictionary *)dic {
    _dic = dic;
    NSDictionary *dicOfUser = [self.dic objectForKey:@"user"];
    UILabel *labelOfGender = [[UILabel alloc]init];
    [self.view addSubview:labelOfGender];
    labelOfGender.text = @"性别";
    labelOfGender.textColor = [UIColor lightGrayColor];
    [labelOfGender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.seg.mas_bottom).with.offset(5);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
    }];
    
    self.gender = [[UILabel alloc]init];
    NSString *sexText = [NSString stringWithFormat:@"%@",[dicOfUser objectForKey:@"gender"]];
    if ([sexText isEqualToString:@"1"]) {
        _gender.text = @"男";
        self.genderImage.image = [UIImage imageNamed:@"icon_sex_0"];
    }else {
        _gender.text = @"女";
        self.genderImage.image = [UIImage imageNamed:@"icon_sex_1"];
    }
    [self.view addSubview:_gender];
    [_gender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.seg.mas_bottom).with.offset(5);
        make.left.equalTo(labelOfGender.mas_right).with.offset(80);
        make.width.mas_equalTo(SCREEN_WIDTH - 120);
        make.height.mas_equalTo(30);
        
    }];
    UILabel *labelOfAge = [[UILabel alloc]init];
    [self.view addSubview:labelOfAge];
    labelOfAge.textColor = [UIColor lightGrayColor];
    labelOfAge.text = @"年龄";
    [labelOfAge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelOfGender.mas_bottom).with.offset(10);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
        
    }];
    self.age = [[UILabel alloc]init];
    [self.view addSubview:_age];
    _age.text =[NSString stringWithFormat:@"%@",[dicOfUser objectForKey:@"age"]];
    [_age mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_gender.mas_bottom).with.offset(10);
        make.left.equalTo(labelOfAge.mas_right).with.offset(80);
        make.width.mas_equalTo(SCREEN_WIDTH - 240);
        make.height.mas_equalTo(30);
        
        
    }];
    
    UILabel *labelOfBithday = [[UILabel alloc]init];
    [self.view addSubview:labelOfBithday];
    labelOfBithday.textColor = [UIColor lightGrayColor];
    labelOfBithday.text = @"出生年月";
    [labelOfBithday mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelOfAge.mas_bottom).with.offset(10);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(30);
        
    }];
    self.bithday = [[UILabel alloc]init];
    [self.view addSubview:_bithday];
    _bithday.text = [dicOfUser objectForKey:@"birthday"];
    [_bithday mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_age.mas_bottom).with.offset(10);
        make.left.equalTo(labelOfBithday.mas_right).with.offset(20);
        make.width.mas_equalTo(SCREEN_WIDTH - 120);
        make.height.mas_equalTo(30);
    }];
    UILabel *labelofConstellation = [[UILabel alloc]init];
    [self.view addSubview:labelofConstellation];
    labelofConstellation.textColor = [UIColor lightGrayColor];
    labelofConstellation.text = @"星座";
    [labelofConstellation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelOfBithday.mas_bottom).with.offset(10);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
        
    }];
    self.constellation = [[UILabel alloc]init];
    [self.view addSubview:_constellation];
    _constellation.text = [dicOfUser objectForKey:@"constellation"];
    [_constellation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bithday.mas_bottom).with.offset(10);
        make.left.equalTo(labelofConstellation.mas_right).with.offset(80);
        make.width.mas_equalTo(SCREEN_WIDTH - 120);
        make.height.mas_equalTo(30);
    }];
    
}
// 注册之后信息
- (void)setInfoDicTwo:(NSDictionary *)infoDicTwo {
    _infoDicTwo = infoDicTwo;
    NSDictionary *dicOfUser = [self.infoDicTwo objectForKey:@"user"];
    UILabel *labelOfGender = [[UILabel alloc]init];
    [self.view addSubview:labelOfGender];
    labelOfGender.text = @"性别";
    labelOfGender.textColor = [UIColor lightGrayColor];
    [labelOfGender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.seg.mas_bottom).with.offset(5);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
    }];
    
    self.gender = [[UILabel alloc]init];
    NSString *sexText = [NSString stringWithFormat:@"%@",[dicOfUser objectForKey:@"gender"]];
    if ([sexText isEqualToString:@"1"]) {
        _gender.text = @"男";
        self.genderImage.image = [UIImage imageNamed:@"icon_sex_0"];
    }else {
        _gender.text = @"女";
        self.genderImage.image = [UIImage imageNamed:@"icon_sex_1"];
    }
    [self.view addSubview:_gender];
    [_gender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.seg.mas_bottom).with.offset(5);
        make.left.equalTo(labelOfGender.mas_right).with.offset(80);
        make.width.mas_equalTo(SCREEN_WIDTH - 120);
        make.height.mas_equalTo(30);
        
    }];
    UILabel *labelOfAge = [[UILabel alloc]init];
    [self.view addSubview:labelOfAge];
    labelOfAge.textColor = [UIColor lightGrayColor];
    labelOfAge.text = @"年龄";
    [labelOfAge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelOfGender.mas_bottom).with.offset(10);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
        
    }];
    self.age = [[UILabel alloc]init];
    [self.view addSubview:_age];
    _age.text =[NSString stringWithFormat:@"%@",[dicOfUser objectForKey:@"age"]];
    [_age mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_gender.mas_bottom).with.offset(10);
        make.left.equalTo(labelOfAge.mas_right).with.offset(80);
        make.width.mas_equalTo(SCREEN_WIDTH - 240);
        make.height.mas_equalTo(30);
        
        
    }];
    
    UILabel *labelOfBithday = [[UILabel alloc]init];
    [self.view addSubview:labelOfBithday];
    labelOfBithday.textColor = [UIColor lightGrayColor];
    labelOfBithday.text = @"出生年月";
    [labelOfBithday mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelOfAge.mas_bottom).with.offset(10);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(30);
        
    }];
    self.bithday = [[UILabel alloc]init];
    [self.view addSubview:_bithday];
    _bithday.text = [dicOfUser objectForKey:@"birthday"];
    [_bithday mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_age.mas_bottom).with.offset(10);
        make.left.equalTo(labelOfBithday.mas_right).with.offset(20);
        make.width.mas_equalTo(SCREEN_WIDTH - 120);
        make.height.mas_equalTo(30);
    }];
    UILabel *labelofConstellation = [[UILabel alloc]init];
    [self.view addSubview:labelofConstellation];
    labelofConstellation.textColor = [UIColor lightGrayColor];
    labelofConstellation.text = @"星座";
    [labelofConstellation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelOfBithday.mas_bottom).with.offset(10);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
        
    }];
    self.constellation = [[UILabel alloc]init];
    [self.view addSubview:_constellation];
    _constellation.text = [dicOfUser objectForKey:@"constellation"];
    [_constellation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bithday.mas_bottom).with.offset(10);
        make.left.equalTo(labelofConstellation.mas_right).with.offset(80);
        make.width.mas_equalTo(SCREEN_WIDTH - 120);
        make.height.mas_equalTo(30);
    }];
    

}
-(void)setInfoDic:(NSDictionary *)infoDic {
    _infoDic = infoDic;
    NSDictionary *dicOfUser = [self.infoDic objectForKey:@"user"];
    UILabel *labelOfGender = [[UILabel alloc]init];
    [self.view addSubview:labelOfGender];
    labelOfGender.text = @"性别";
    labelOfGender.textColor = [UIColor lightGrayColor];
    [labelOfGender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.seg.mas_bottom).with.offset(5);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
    }];
    
    self.gender = [[UILabel alloc]init];
    NSString *sexText = [NSString stringWithFormat:@"%@",[dicOfUser objectForKey:@"gender"]];
    if ([sexText isEqualToString:@"1"]) {
        _gender.text = @"男";
        self.genderImage.image = [UIImage imageNamed:@"icon_sex_0"];
    }else {
        _gender.text = @"女";
        self.genderImage.image = [UIImage imageNamed:@"icon_sex_1"];
    }
    [self.view addSubview:_gender];
    [_gender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.seg.mas_bottom).with.offset(5);
        make.left.equalTo(labelOfGender.mas_right).with.offset(80);
        make.width.mas_equalTo(SCREEN_WIDTH - 120);
        make.height.mas_equalTo(30);
        
    }];
    UILabel *labelOfAge = [[UILabel alloc]init];
    [self.view addSubview:labelOfAge];
    labelOfAge.textColor = [UIColor lightGrayColor];
    labelOfAge.text = @"年龄";
    [labelOfAge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelOfGender.mas_bottom).with.offset(10);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
        
    }];
    self.age = [[UILabel alloc]init];
    [self.view addSubview:_age];
    _age.text =[NSString stringWithFormat:@"%@",[dicOfUser objectForKey:@"age"]];
    [_age mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_gender.mas_bottom).with.offset(10);
        make.left.equalTo(labelOfAge.mas_right).with.offset(80);
        make.width.mas_equalTo(SCREEN_WIDTH - 240);
        make.height.mas_equalTo(30);
        
        
    }];
    
    UILabel *labelOfBithday = [[UILabel alloc]init];
    [self.view addSubview:labelOfBithday];
    labelOfBithday.textColor = [UIColor lightGrayColor];
    labelOfBithday.text = @"出生年月";
    [labelOfBithday mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelOfAge.mas_bottom).with.offset(10);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(30);
        
    }];
    self.bithday = [[UILabel alloc]init];
    [self.view addSubview:_bithday];
    _bithday.text = [dicOfUser objectForKey:@"birthday"];
    [_bithday mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_age.mas_bottom).with.offset(10);
        make.left.equalTo(labelOfBithday.mas_right).with.offset(20);
        make.width.mas_equalTo(SCREEN_WIDTH - 120);
        make.height.mas_equalTo(30);
    }];
    UILabel *labelofConstellation = [[UILabel alloc]init];
    [self.view addSubview:labelofConstellation];
    labelofConstellation.textColor = [UIColor lightGrayColor];
    labelofConstellation.text = @"星座";
    [labelofConstellation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelOfBithday.mas_bottom).with.offset(10);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
        
    }];
    self.constellation = [[UILabel alloc]init];
    [self.view addSubview:_constellation];
    _constellation.text = [dicOfUser objectForKey:@"constellation"];
    [_constellation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bithday.mas_bottom).with.offset(10);
        make.left.equalTo(labelofConstellation.mas_right).with.offset(80);
        make.width.mas_equalTo(SCREEN_WIDTH - 120);
        make.height.mas_equalTo(30);
    }];
    

}
- (void)handleValue:(UISegmentedControl *)seg {
    switch (seg.selectedSegmentIndex) {
        case 0:
            NSLog(@"11");
            break;
            
        default:
            NSLog(@"2");
            break;
    }
}
- (void)handle:(UIButton *)button {
    JYJVCOfSetContent *setContent = [[JYJVCOfSetContent alloc]init];
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:setContent animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
- (void)handlePic:(UITapGestureRecognizer *)tap{
    UIAlertController *altert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从手机相册选择" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel  handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [altert addAction:action1];
    [altert addAction:action2];
    [altert addAction:action3];
   [self presentViewController:altert animated:YES completion:^{
       
   }];
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (image != nil) {
        _imageOfHead.image = image;
    }
    NSData *data;
    if (UIImagePNGRepresentation(image)) {
        data = UIImagePNGRepresentation(image);
    }else {
        data = UIImageJPEGRepresentation(image, 1.0);
    }
    [[NSFileManager defaultManager] createFileAtPath:_imgPath contents:data attributes:nil];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:data forKey:@"image"];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
